package com.gsafety.wellsfort.service.knowledge;

import cn.jiguang.common.resp.APIRequestException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.enums.PrivilegeEnum;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.dao.knowledge.TeachaidPrivilegeDAO;
import com.gsafety.wellsfort.dao.knowledge.TeachingAidDAO;
import com.gsafety.wellsfort.dao.security.AdminDAO;
import com.gsafety.wellsfort.dao.security.AdminRoleRelDAO;
import com.gsafety.wellsfort.dao.security.MenuDAO;
import com.gsafety.wellsfort.dao.security.RoleDAO;
import com.gsafety.wellsfort.domain.dto.teachaid.TeachingAidDTO;
import com.gsafety.wellsfort.domain.dto.teachaid.TeachingAidQueryDTO;
import com.gsafety.wellsfort.domain.model.knowledge.TeachaidPrivilege;
import com.gsafety.wellsfort.domain.model.knowledge.TeachingAid;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.model.security.AdminRoleRel;
import com.gsafety.wellsfort.domain.model.security.Menu;
import com.gsafety.wellsfort.domain.model.security.Role;
import com.gsafety.wellsfort.domain.vo.CoursewareVO;
import com.gsafety.wellsfort.domain.vo.TeachaidPrivilegeVO;
import com.gsafety.wellsfort.domain.vo.TeachingAidVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class TeachingAidService {
    @Autowired(required = false)
    TeachingAidDAO teachingAidDAO;
    @Autowired(required = false)
    TeachaidPrivilegeDAO privilegeDAO;
    @Autowired(required = false)
    RoleDAO roleDAO;
    @Autowired(required = false)
    MenuDAO menuDAO;
    @Autowired(required = false)
    AdminDAO adminDAO;
    @Autowired(required = false)
    AdminRoleRelDAO adminRoleRelDAO;

    public ApiResponse<TeachingAidDTO> saveTeachingAid(TeachingAidDTO teachingAidDTO) {
        ApiResponse apiResponse = null;
        TeachingAid teachingAid = new TeachingAid();
        BeanUtils.copyProperties(teachingAidDTO, teachingAid);
        List<TeachaidPrivilege> privilegeList = teachingAidDTO.getTeachaidPrivilege();
        Integer teachingAidId = teachingAidDTO.getId();
        teachingAid.setModified(LoginContextUtil.getAdmin().getId());
        teachingAid.setUpdateTime(new Date());
        if (teachingAidId != null) {
            privilegeDAO.deleteByTeachaidId(teachingAidId);
            teachingAidDAO.updateByPrimaryKeySelective(teachingAid);
        } else {
            teachingAid.setCreator(LoginContextUtil.getAdmin().getId());
            teachingAid.setCreateTime(new Date());
            teachingAidDAO.insert(teachingAid);
            teachingAidDTO.setId(teachingAid.getId());
        }
        if (privilegeList != null) {
            for (TeachaidPrivilege privilege : privilegeList) {
                privilege.setTeachaidId(teachingAid.getId());
                privilegeDAO.insert(privilege);

            }
        }
        apiResponse = ApiResponse.success(teachingAidDTO);
        return apiResponse;
    }

    public ApiResponse deleteByTeachingAidId(Integer id) {
        ApiResponse apiResponse = null;
        try {
            privilegeDAO.deleteByTeachaidId(id);
            teachingAidDAO.deleteByPrimaryKey(id);
            apiResponse = ApiResponse.success("教具删除成功");
        } catch (Exception e) {
            apiResponse = ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }

    public ApiResponse<TeachingAidVO> getTeachingAid(Integer id) {
        ApiResponse<TeachingAidVO> apiResponse = null;
        try {

            List<TeachaidPrivilegeVO> privilegeVOList = new ArrayList<TeachaidPrivilegeVO>();
            List<TeachaidPrivilege> privilegeList = privilegeDAO.selectByTeachaidId(id);
            for (TeachaidPrivilege privilege : privilegeList) {
                TeachaidPrivilegeVO privilegeVO = new TeachaidPrivilegeVO();
                BeanUtils.copyProperties(privilege, privilegeVO);
                Role r = roleDAO.selectByPrimaryKey(privilege.getRoleId());
                Menu m = menuDAO.selectByPrimaryKey(privilege.getMenuId());
                if (m != null) privilegeVO.setMenuPrivilegeName(m.getName());
                if (r != null) privilegeVO.setRoleName(r.getName());
                privilegeVOList.add(privilegeVO);
            }
            TeachingAid teachingAid = teachingAidDAO.selectByPrimaryKey(id);
            TeachingAidVO teachingAidVo = new TeachingAidVO.VOBuilder(teachingAid).build();
            if (teachingAidVo.getModified() != null) {
                Admin admin = adminDAO.selectByPrimaryKey(teachingAidVo.getModified());
                teachingAidVo.setModifiedName(admin.getName());
            }

            teachingAidVo.setTeachaidPrivilege(privilegeVOList);
            apiResponse = ApiResponse.success(teachingAidVo);
        } catch (Exception e) {
            apiResponse = ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }

    public ApiResponse<List<TeachingAidVO>> findByPageParams(TeachingAidQueryDTO dtoParams) {
        ApiResponse<List<TeachingAidVO>> apiResponse = null;
        Admin admin = LoginContextUtil.getAdmin();
        if (dtoParams.getResourceScope() != null && dtoParams.getResourceScope() != 0) {
            if (admin != null) dtoParams.setCreator(LoginContextUtil.getAdmin().getId());
        }
        Page page = PageHelper.startPage(dtoParams.getPageNo(), dtoParams.getPageSize());
        List<TeachingAid> resultList = teachingAidDAO.selectByParam(dtoParams);
        List<TeachingAidVO> voList = new ArrayList<>();
        for (TeachingAid q : resultList) {
            ApiResponse<TeachingAidVO> response = getTeachingAid(q.getId());
            if (response.getData() != null) {
                TeachingAidVO teachingAidVO = response.getData();
                voList.add(teachingAidVO);
            }

        }
        PageInfo pageInfo = new PageInfo(page);
        pageInfo.setList(resultList);
        apiResponse = ApiResponse.success(voList, (int) pageInfo.getTotal());
        return apiResponse;
    }

    public ApiResponse<List<TeachingAid>> getAllByRole() {
        Admin admin = LoginContextUtil.getAdmin();
        if (admin == null) {
            return ApiResponse.error(ResultCode.INVALID_PARAM);
        }
        List<AdminRoleRel> adminRoleRelList = adminRoleRelDAO.selectByAdminId(admin.getId());
        if (adminRoleRelList == null) {
            return ApiResponse.error(ResultCode.INVALID_PARAM);
        }
        List<TeachaidPrivilege> teachAidPrivilegeList = privilegeDAO.selectByParam(adminRoleRelList.get(0).getRoleId(), PrivilegeEnum.TEACHING_AID_USE.getMenuId());
        List<TeachingAid> list = new ArrayList<>();
        if (teachAidPrivilegeList != null && teachAidPrivilegeList.size() > 0) {
            for (TeachaidPrivilege teachaidPrivilege : teachAidPrivilegeList) {
                if (teachaidPrivilege != null) {
                    TeachingAid teachingAid = teachingAidDAO.selectByPrimaryKey(teachaidPrivilege.getTeachaidId());
                    if (teachingAid != null && teachingAid.getIsOpen() == 1) {
                        list.add(teachingAid);
                    }
                }
            }
        }
        return ApiResponse.success(list);
    }
}
